﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RepairChecker
{
    public partial class jobstart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderUI();
            }

        }

        void RenderUI()
        {
            List<District> districts = Entity.GetDistricts(false);
            lstAddDistrict.Items.Clear();
            lstAddDistrict.Items.Add("");
            foreach (District d in districts)
            {
                lstAddDistrict.Items.Add(new ListItem(d.DistrictName, d.DistrictId.ToString()));
            }

            clstFault.Items.Clear();
            List<Fault> faults = Entity.GetAllFaults();
            foreach (Fault f in faults)
            {
                clstFault.Items.Add(new ListItem(f.FaultName, f.FaultId.ToString()));
            }



        }

        protected void lstAddDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Office> offices = Entity.GetOffices(int.Parse(lstAddDistrict.SelectedValue), false);
            lstOffice.Items.Clear();
            lstOffice.Items.Add("");
            foreach (Office o in offices)
            {
                lstOffice.Items.Add(new ListItem(o.OfficeName, o.OfficeId.ToString()));
            }
            dataUnfinished.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (lstOffice.SelectedValue == "")
            {
                lblError.Text = "选择网点";
                return;
            }
            if (clstFault.SelectedIndex == -1)
            {
                lblError.Text = "选择故障类型";
                return;
            }
            string faultIdString = string.Empty;
            foreach (ListItem item in clstFault.Items)
            {
                if (item.Selected==true)
                {
                    faultIdString += item.Value + ',';
                }
            }

            int ret=
                Entity.AddJob(int.Parse(lstOffice.SelectedValue), faultIdString, Entity.JobType.Repair, txtMemo.Text);
            if (ret == -1)
            {
                lblError.Text = "提交出错";
            }
            else
            {
                dataUnfinished.DataBind();
                lstAddDistrict.SelectedIndex = 0;
                lstOffice.SelectedIndex = 0;
                clstFault.ClearSelection();
                //clstFault.SelectedIndex = -1;
            }
        }


        protected void dataUnfinished_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int LineNum = int.Parse(e.CommandArgument.ToString());


            if (e.CommandName=="btnDelete")
            {

                int ret=
                    Entity.DeleteJob((int)dataUnfinished.DataKeys[LineNum].Values["FormId"]);

                if (ret!=0)
                {
                    dataUnfinished.DataBind();
                }
                return;
            }

            if (e.CommandName == "btnNotify")
            {
                
                DropDownList _lstNotifyCrewName = (DropDownList)dataUnfinished .Rows[LineNum].FindControl("lstNotifyCrewName");

                if (_lstNotifyCrewName.SelectedValue=="0")
                {
                    return;
                    //lblError.Text = "";
                }
                
                Entity.SetNotified(
                    int.Parse(dataUnfinished.DataKeys[LineNum].Values["FormId"].ToString()),
                    int.Parse(_lstNotifyCrewName.SelectedValue));

                dataUnfinished.DataBind();

            }
        }


        protected void dataUnfinished_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //insert all crews to ddlist and set default crew
                DropDownList _lstNotifyCrewName = (DropDownList)e.Row.FindControl("lstNotifyCrewName");
                Button btn = (Button)e.Row.FindControl("btnNotify");
                List<Crew> crews = Entity.GetAllCrews();
                _lstNotifyCrewName.Items.Add(new ListItem("","0"));
                foreach (Crew crew  in crews)
                {
                    _lstNotifyCrewName.Items.Add(new ListItem(crew.CrewName, crew.CrewId.ToString()));
                }
                Job job=(Job)e.Row.DataItem;
                //_lstNotifyCrewName.SelectedValue = job.CrewId.ToString();

                if (!String.IsNullOrEmpty(job.FaultIdString))
                {
                    BulletedList _lstFaults = (BulletedList)e.Row.FindControl("lstFaults");
                    List<Fault> faults = Entity.GetAllFaults();
                    
                    foreach (string s in job.FaultIdString.Split(','))
                    {
                        if (!string.IsNullOrEmpty(s)) //
                        {
                            foreach (Fault f in faults)
                            {
                                if (f.FaultId==int.Parse(s))
                                {
                                    _lstFaults.Items.Add(f.FaultName);
                                    break;
                                }
                            }
                        }
                       
                    }
                }

                
                if (job.JobStatus==2)
                {
                    if (e.Row.Cells[e.Row.Cells.Count-1].Controls[0].GetType().ToString().Equals("System.Web.UI.WebControls.DataControlButton"))
                    {
                        e.Row.Cells[e.Row.Cells.Count - 1].Controls.RemoveAt(0);
                    }
                }


            }
        }




    }
}